<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\CompanyRecharge;
use App\Admin\Repositories\Company;
use App\Models\CategoryModel;
use App\Models\CompanyModel;
use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use App\Services\Easemob\EasemobService;
use App\Services\Im\WechatService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CompanyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Company(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('mobile', '注册账号');
            $grid->column('company_name');
            $grid->column('is_lock', '状态')
                ->using(CompanyModel::VERIFY_LIST)
                ->dot(
                    [
                        CompanyModel::VERIFY_WAIT => 'success',
                        CompanyModel::VERIFY_WAITING => 'danger',
                    ],
                    'primary'
                );
            $grid->column('registration_num', '报名单余额');

            $grid->column('cur_type', '类型')->using(CompanyModel::TYPE_LIST);
            $grid->column('business_license_photo')->image();
            $grid->column('company_logo')->image('', 100, 100);
            $grid->column('identity_card_front', '身份证人像面')->image();
            $grid->column('identity_card_back', '身份证国会面')->image();
            $grid->column('contact_person_avatar')->image('', 100, 100);
            $grid->column('contact_person_name');
            $grid->column('is_show_mobile', '报名是否展示手机号')->using(CompanyModel::SHOW_MOBILE_LIST);


            $grid->column('updated_at')->sortable();

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('is_lock', '状态', CompanyModel::VERIFY_LIST);
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->like('company_name')->width(2);
                $filter->like('contact_person_name')->width(2);
                $filter->equal('mobile', '注册账号')->width(2);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new CompanyRecharge());
                $url = admin_url("registration-log?company_id=$this->id");
                $actions->append("<a href='" . $url . "' class='btn btn-success btn-sm' style='margin-left: 5px'>报名单日志</a>");
            });
            $grid->fixColumns(0, -1);
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            //边框模式
            $grid->withBorder();
            //横向滚动条
            $grid->scrollbarX();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            $grid->disableDeleteButton();
            // 禁用显示按钮
            $grid->disableViewButton();
            $grid->disableRowSelector();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Company(), function (Form $form) {
            //TODO --
            //修改环信昵称和头像
            // if (!empty($form->model()->uuname)) {
            //     $EasemobService = new EasemobService;
            //     if (!is_null($form->contact_person_name)) {
            //         $EasemobService->attribute($form->model()->uuname, ["nickname" => $form->contact_person_name]);
            //     }
            //     if (!is_null($form->contact_person_avatar)) {
            //         $EasemobService->attribute($form->model()->uuname, ["avatarurl" => $form->contact_person_avatar]);
            //     }
            // }
            $form->display('id');
            $form->radio('cur_type', '类型')->options(CompanyModel::TYPE_LIST)->required()
                ->when(CompanyModel::TYPE_PERSON, function (Form $form) {
                    //个人
                    $form->image('identity_card_front', '身份证人像面')->uniqueName()->autoUpload()->removable(false)->saveFullUrl()->autoSave(false);
                    $form->image('identity_card_back', '身份证国徽面')->uniqueName()->autoUpload()->removable(false)->saveFullUrl()->autoSave(false);
                })->when(CompanyModel::TYPE_ENTERPRISE, function (Form $form) {
                    $form->text('company_name', '名称')->rules('required_if:cur_type,2', [
                        'required_if' => '名称不能为空',
                    ]);
                    $form->image('business_license_photo')->uniqueName()->autoUpload()->removable(false)->saveFullUrl()->autoSave(false);
                    $form->image('company_logo')->uniqueName()->autoUpload()->removable(false)->saveFullUrl()->autoSave(false);
                })->default(CompanyModel::TYPE_ENTERPRISE);
            $form->text('contact_person_name');
            $form->image('contact_person_avatar')->uniqueName()->autoUpload()->removable(false)->saveFullUrl();
            $form->text('mobile', '注册账号')->default('');
            $form->radio('is_lock', '状态')->options(CompanyModel::VERIFY_LIST)->default(CompanyModel::VERIFY_WAIT);
            $form->radio('is_show_mobile', '是否展示报名手机号')->options(CompanyModel::SHOW_MOBILE_LIST)->default(CompanyModel::SHOW_MOBILE_OFF);
            $form->text('password', "登录密码")->minLength(6)->maxLength(32)->customFormat(function ($v) {
                if ($v == $this->password) {
                    return;
                }
                return $v;
            });
            $form->display('created_at');
            $form->display('updated_at');
            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
                if (!$form->password) {
                    $form->deleteInput('password');
                }
                // if($form->is_lock == 3){
                //     JobpositionModel::query()->where("company_id",$form->model()->id)->update(["cur_status"=>2]);
                // }
            });

            $form->saved(function (Form $form, $result) {
                if ($form->isCreating()) {
                    $newId = $result;
                    $wechatIm = new WechatService;
                    $wechatIm->create('company_' . $newId, 'company_' . $newId, $form->contact_person_avatar);
                    CompanyModel::query()->where("id", $newId)->update(["is_im" => 1]);
                }
            });

            //禁用显示按钮
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();
            $form->disableViewCheck();
        });
    }
}
